package com.hmall.item.task;

import com.alibaba.fastjson.JSON;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * x
 *
 * @Description TopTask
 * @Author kzw
 * @Date 2024-11-14
 */
@Slf4j
@Component
@EnableScheduling
public class TopTask {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ItemMapper itemMapper;

    @Scheduled(cron = "0 0 3 * * ? ")
    public void saveTopProductsToRedis() {
        log.info("定时任务开始执行");
        List<Item> topProducts = itemMapper.searchListTop();
        // 将销量 Top20 的商品列表保存到 Redis 中，使用一个特定的键
        redisTemplate.opsForValue().set("top20", JSON.toJSONString(topProducts));
    }
}